﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TienTeTuong.back_end
{
    public partial class EditCategoryDetail : System.Web.UI.Page
    {
        public CategoryDetailDTO item = new CategoryDetailDTO();
        public List<CategoryDTO> Cates = new List<CategoryDTO>();
        public CategoryBUS cateBus = new CategoryBUS();
        public CategoryDetailBUS ctBus = new CategoryDetailBUS();
        public ProductDetailBUS proBus = new ProductDetailBUS();
        public List<CateDetailProductDTO> opt = new List<CateDetailProductDTO>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Admin"] == null)
            {
                Response.Redirect("Default.aspx");
            }

            Cates = cateBus.GetListCategory();
            if (Page.IsPostBack)
            {
                string id = Request.Params["id"] ?? "";
                item = ctBus.GetCategoryDetailByID(id);
                item.Name = txtName.Text;
                item.Description = txtDescription.Text;
                item.Active = bool.Parse(isActive.Value);
                item.Hot = bool.Parse(isHot.Value);
                if (Session["img"] != null)
                {
                    item.Image = Session["img"].ToString();
                    Session["img"] = null;
                }
                if (Session["img1"] != null)
                {
                    item.Image2 = Session["img1"].ToString();
                    Session["img1"] = null;
                }
                if (Session["img2"] != null)
                {
                    item.Image3 = Session["img2"].ToString();
                    Session["img2"] = null;
                }
                item.Price = 0;
                proBus.deleteCateDetailProductByCateDetailID(item.CateDetailID);
                item.CateID = CateSelected.Value;
                List<CateDetailProductDTO> newOpt = new List<CateDetailProductDTO>();
                if (selected.Value != "")
                {
                    List<String> arrAddPD = selected.Value.Split('/').ToList();
                    List<String> arrCount = selectedCount.Value.Split('/').ToList();
                    for (var i = 0; i < arrAddPD.Count; i++ )
                    {
                        if (arrAddPD[i] != "")
                        {
                            var proDetail = proBus.getProductDetailByID(arrAddPD[i]);
                            if (proDetail != null)
                            {
                                double tmpPrice = 0;
                                double quantitty = 0;
                                Double.TryParse(proDetail.Price, out tmpPrice);
                                Double.TryParse(arrCount[i], out quantitty);
                                item.Price += tmpPrice*quantitty;
                                proDetail.Quantity = quantitty;
                                newOpt.Add(proDetail);

                                proBus.updateCateDetailProduct(proDetail.ProId, item.CateDetailID, quantitty.ToString());
                            }
                        }
                    }
                }
                ctBus.update(item);
                Response.Redirect("categorydetailmanagement.aspx");
            }
            else
            {
                string id = Request.Params["id"] ?? "";
                item = ctBus.GetCategoryDetailByID(id);
                Session["img"] = null;
                opt = proBus.GetListProductDetail(item.CateDetailID);
                txtName.Text = item.Name;
                txtDescription.Text = item.Description;
                isHot.Value = item.Hot.ToString();
                isActive.Value = item.Active.ToString();
            }
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {

        }
    }
}